﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ScrumTable.BL.DM.Properties;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.DL.Data.Common.Serialization;
using ScrumTable.DL.Data.Schema;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{
     
    /// <summary>
    /// Contains the basic information about a task.
    /// </summary>
    public class Task : ScrumItemBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets/sets the estimated time in hours.
        /// </summary>
        public double? Estimate
        {
            get { return DataObject.GetData(Schema.TaskSchema.Estimate, data => (double)data, new double?()); }
            set { SetDataWithUndoAction(Schema.TaskSchema.Estimate, value); }
        }

        /// <summary>
        /// Gets/sets the remaining work time in hours.
        /// </summary>
        public double? RemainingWork
        {
            get { return DataObject.GetData(Schema.TaskSchema.RemainingWork, data => (double)data, new double?()); }
            set { SetDataWithUndoAction(Schema.TaskSchema.RemainingWork, value); }
        }

        /// <summary>
        /// Gets/sets the completed work time in hours.
        /// </summary>
        public double? CompletedWork
        {
            get { return DataObject.GetData(Schema.TaskSchema.CompletedWork, data => (double)data, new double?()); }
            set { SetDataWithUndoAction(Schema.TaskSchema.CompletedWork, value); }
        }

        /// <summary>
        /// Gets/sets the task priority.
        /// </summary>
        public int Priority
        {
            get { return DataObject.GetData<int>(Schema.TaskSchema.Priority); }
            set { SetDataWithUndoAction(Schema.TaskSchema.Priority, value); }
        }

        /// <summary>
        /// Gets the ScrumItemType of the ScrumItem
        /// </summary>
        public override  ScrumItemType ItemType
        { 
            get { return ParentProject.Types.TaskType; }
        }
        
        /// <summary>
        /// Gets the parent collection instance.
        /// </summary>
        internal override IDomainCollection ParentCollection
        {
            get { return Parent.SearchCollection<Task>(); }
        }


        /// <summary>
        /// Gets the domain object schema of the current domain object.
        /// </summary>
        protected override SchemaBase DomainSchema { get { return Schema.TaskSchema; } }

        /// <summary>
        /// Returns the name-field schema information.
        /// </summary>
        protected override SchemaBase NameSchema { get { return Schema.TaskSchema.Name; } }

        /// <summary>
        /// Returns the description-field schema information.
        /// </summary>
        protected override SchemaBase DescriptionSchema { get { return Schema.TaskSchema.Description; } }

        /// <summary>
        /// Returns the memberid-field schema information.
        /// </summary>
        protected override SchemaBase AssignedMemberSchema { get { return Schema.TaskSchema.MemberId; } }

        /// <summary>
        /// Returns the ranking-field schema information.
        /// </summary>
        protected override SchemaBase RankingSchema { get { return Schema.TaskSchema.Ranking; } }

        /// <summary>
        /// Returns the state-field schema information.
        /// </summary>
        protected override SchemaBase StateSchema { get { return Schema.TaskSchema.State; } }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns true if the specified state change could be accomplished.
        /// </summary>
        /// <param name="newState">Specifies the new state to validate (to).</param>
        public override bool CanChangeState(ScrumState newState)
        {
            return (base.CanChangeState(newState) && ParentProject.Types.TaskType.CanChangeState(State, newState));
        }

        /// <summary>
        /// Initializes the mappings stored in the current instance.
        /// </summary>
        protected override void InitializeMappings()
        {
            base.InitializeMappings();

            PropertySchemaMappings.Add(Schema.TaskSchema.Priority, "Priority", true);
            PropertySchemaMappings.Add(Schema.TaskSchema.RemainingWork, "RemainingWork", true);
            PropertySchemaMappings.Add(Schema.TaskSchema.CompletedWork, "CompletedWork", true);
            PropertySchemaMappings.Add(Schema.TaskSchema.Estimate, "Estimate", true);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}